#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>

#include "frame.h"
#include "cache.h"

int runflag = 1;

void *producer_thread(void *arg)
{
    FILE *fp = fopen("producer.log","w");
    char devid[32];
    memcpy(devid,arg,sizeof(devid));
    fprintf(fp,">>>producer_thread devid = %s\n",devid);
    //fprintf(stderr,">>>producer_thread devid = %s\n",devid);

    unsigned char buf[8192]={0};
    int len = 8192,ret,flag;
    while(runflag)
    {
	len = rand()%sizeof(buf);
	ret = cache_write_frame(devid,-1,buf,len,CACHE_FLAG_STREAM|FRAME_FLAG_IUNIT);
	if(ret == CACHE_CLEAR_FLAG)
	{
	    break;
	}
	fprintf(fp,"++++++writer IUNIT len = %d,ret = %d\n",len,ret);

	ret = cache_write_frame(devid,0,buf,len,CACHE_FLAG_TOAPPD|FRAME_FLAG_CTCMD);
	if(ret == CACHE_CLEAR_FLAG)
	{
	    break;
	}
	fprintf(fp,"++++++writer CTCMD RID[0] len = %d,ret = %d\n",len,ret);

	flag = CACHE_FLAG_TODEVD|FRAME_FLAG_CTCMD;
	len = cache_read_frame(devid,0,buf,sizeof(buf),&flag);
	fprintf(fp,"---devreader CTCMD len = %d,flag = %#x\n",len,flag);

	sleep(1);
    }
    cache_index_del(devid);
    fflush(fp);
    fclose(fp);
    return (void*)NULL;
}

void *consumer_thread(void *arg)
{
    char devid[32];
    memcpy(devid,arg,sizeof(devid));

    int rid = -1;
    rid = cache_add_reader(devid);
    if(rid<0)
    {
	return (void*)-1;
    }
    //while(runflag && rid<0)
    //{
    //    rid = cache_add_reader(devid);
    //    sleep(1);
    //}
    char fname[32];
    snprintf(fname,sizeof(fname),"consumer_%02d.log",rid&0xff);
    FILE *fp = fopen(fname,"w");
    fprintf(fp,">>>consumer devid = %s, reader ID = %d\n",devid,rid);
    //fprintf(stderr,">>>consumer devid = %s, reader ID = %d\n",devid,rid);

    unsigned char buf[8192] = {0};
    int size = sizeof(buf);
    int len = 0,ret;
    int flag = CACHE_FLAG_STREAM|FRAME_FLAG_IUNIT|FRAME_FLAG_PUNIT;
    while(runflag)
    {
	flag = CACHE_FLAG_STREAM|FRAME_FLAG_IUNIT|FRAME_FLAG_PUNIT;
	len = cache_read_frame(devid,rid,buf,size,&flag);
	fprintf(fp,"---reader[%#x] Video len = %d,flag = %#x\n",rid,len,flag);

	flag = CACHE_FLAG_TOAPPD|FRAME_FLAG_CTCMD;
	len = cache_read_frame(devid,rid,buf,size,&flag);
	fprintf(fp,"---reader[%#x] CTCMD len = %d,flag = %#x\n",rid,len,flag);
	
	ret = cache_write_frame(devid,0,buf,len,CACHE_FLAG_TODEVD|FRAME_FLAG_CTCMD);
	fprintf(fp,"++++++write to device CTCMD len = %d,ret = %d\n",len,ret);
	sleep(1);
    }
    cache_del_reader(devid,rid);
    fflush(fp);
    fclose(fp);
    return (void*)NULL;
}

void sig_exit(int signo)
{
    if(signo==SIGINT)
	runflag = 0;
}

int main(int argc, char *argv[])
{
    const char *devid = "MEIA-000001-JYKYM";
    signal(SIGINT,sig_exit);
    cache_index_init(100);

    int ret = 0;
    ret = cache_index_add(devid);
    if(ret < 0)
    {
	fprintf(stderr,"cache_index_add fail,ret = %d\n",ret);
	exit(EXIT_FAILURE);
    }
    pthread_t producer_tid,consumer_tid[5];
    pthread_create(&producer_tid,NULL,producer_thread,(void*)devid);
    int i = 0, reader_cnt = 1;
    for(i=0;i<reader_cnt;i++)
    {
        pthread_create(&consumer_tid[i],NULL,consumer_thread,(void*)devid);
    }

    while(runflag)
    {
	sleep(1);
    }

    pthread_join(producer_tid,NULL);
    for(i=0;i<reader_cnt;i++)
    {
	pthread_join(consumer_tid[i],NULL);
    }

    cache_index_exit();
    exit(EXIT_SUCCESS);
}

